public class MaxSubarraySum {
    public static int maxSubArray(int[] nums) {
        if (nums == null || nums.length == 0) {
            throw new IllegalArgumentException("数组不能为空");
        }
        int currentMax = nums[0];
        int globalMax = nums[0];
        for (int i = 1; i < nums.length; i++) {
            currentMax = Math.max(nums[i], currentMax + nums[i]);
            globalMax = Math.max(globalMax, currentMax);
        }
        return globalMax;
    }
    public static void main(String[] args) {
        int[] testCase1 = {1, -2, 3, 5, -1};
        int[] testCase2 = {1, -2, 3, -8, 5, 1};
        int[] testCase3 = {1, -2, 3, -2, 5, 1};
        int[] testCase4 = {-5, -3, -1}; 
        int[] testCase5 = {-2, 0, -1};  
        System.out.println("数组 [1, -2, 3, 5, -1] 的最大子数组和为：" + maxSubArray(testCase1));
        System.out.println("数组 [1, -2, 3, -8, 5, 1] 的最大子数组和为：" + maxSubArray(testCase2));
        System.out.println("数组 [1, -2, 3, -2, 5, 1] 的最大子数组和为：" + maxSubArray(testCase3));
        System.out.println("数组 [-5, -3, -1] 的最大子数组和为：" + maxSubArray(testCase4));
        System.out.println("数组 [-2, 0, -1] 的最大子数组和为：" + maxSubArray(testCase5));
    }
}
